using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Speed;
using SpeedSearch.Forms;
using Lucene.Net.Search;
using Lucene.Net.Documents;
using SpeedSearch.FullText;

namespace SpeedSearch
{

    public static class IndexHelper
    {

        public static string Extensions = "*.txt, *.html, *.xaml, *.cs, *.xml, *.resx, *.resources, *.css, *.js, *.aspx, *.asax, *.config, *.Master, *.ascx, *.ashx, *.sql, *.sln, *.csproj, *.settings, *.wsdl, *.svc, asmx, *.vbs, *.xsd, *.TXT, *.rdlc, *.SQL, *.ini, *.Sql, *.cnn, *.XML";

        public static void CreateIndexDev(Database db, string dirdev)
        {
            // Todo diretório Dev
            string name = Path.GetFileName(dirdev);
            Config.IndexesRow rowa = NewIndex(db, name, Extensions);
            NewDirectory(db, rowa.IndexId, name, dirdev);

            // 1 índice pra cada versão
            var branches = FileHelper.GetVersions(dirdev);
            foreach (var pairi in branches)
            {
                BrancheInfo branche = pairi.Value;
                Config.IndexesRow rowi = NewIndex(db, branche.VersionName, Extensions);

                foreach (var pairs in branche.Systems)
                {
                    SystemInfo system = pairs.Value;
                    Config.DirectoriesRow rowd = NewDirectory(db, rowi.IndexId, system.SystemName, system.Path);
                }
            }
            db.Save();
        }

        public static Config.IndexesRow NewIndex(Database db, string indexName, string extensions)
        {
            Config.IndexesRow rowi = db.Indexes.SingleOrDefault(p => p.IndexName == indexName);
            if (rowi == null)
            {
                rowi = db.Indexes.NewIndexesRow();
                rowi.IndexName = indexName;
                rowi.Path = Program.GetIndexFile(rowi.IndexName);
                rowi.DateCreated = DateTime.Now;
                rowi.Extensions = extensions;
                db.Indexes.AddIndexesRow(rowi);
            }
            return rowi;
        }

        public static Config.DirectoriesRow NewDirectory(Database db, int indexId, string dirName, string path)
        {
            Config.DirectoriesRow rowd = db.Directories.SingleOrDefault(p => p.IndexId == indexId && p.DirName == dirName);
            if (rowd == null)
            {
                rowd = db.Directories.NewDirectoriesRow();
                rowd.IndexId = indexId;
                rowd.DirName = dirName;
                rowd.Path = path;
                db.Directories.AddDirectoriesRow(rowd);
            }
            return rowd;
        }

        /*
        public static void ReindexAll(Action<string> progress = null)
        {
            foreach (var rowi in Program.Db.Indexes)
            {
                Reindex(rowi);
            }
        }
        */

    }

    public class IndexProgress
    {
        public string MesageIndex ;
        public string MesageDirectory;
        public string MesageCurrent;
        public int IndexCount;
        public int IndexCurrent;
        public int FileCount;
        public int FileCurrent;
        public int DeleteCount;
        public int DeleteCurrent;
        public Exception Exception;
        public bool IsRunning;

        public string LastMessageIndex;

        public int TotalFileCount;
        public int TotalFileAdd;
        public int TotalFileChanged;
        public int TotalFileUnchanged;
        public int TotalDeleteCount;
    }

}
